﻿Imports System.Data
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports System.Data.OleDb
Imports System
Imports System.Globalization

Partial Class Interfaz_frmSistemaPersonal
    Inherits System.Web.UI.Page
    Dim CL As New ControladorLogica
    Dim dtDatos As New DataTable
    Dim dataset As New DataSet

    Dim dtDatosMsj As New DataTable
    Dim datasetMsj As New DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If CL.visibilidadesFormsInternos("Sistema", Session("user").ToString) = False Then
            Response.Redirect("Index.aspx")
        End If

        If Session("personal") Is Nothing Then
            Session("personal") = dtDatos
        Else
            dtDatos = DirectCast(Session("personal"), DataTable)
        End If

        If Session("mensaje") Is Nothing Then
            Session("mensaje") = dtDatos
        Else
            dtDatos = DirectCast(Session("mensaje"), DataTable)
        End If

        If Not Page.IsPostBack Then
            Try
                dtDatos.Columns.Add(New DataColumn("ID"))
                dtDatos.Columns.Add(New DataColumn("FECHA"))
                dtDatos.Columns.Add(New DataColumn("SOLICITANTE"))
                dtDatos.Columns.Add(New DataColumn("DETALLE"))
                dtDatos.Columns.Add(New DataColumn("CENTCOSTO"))
                dtDatos.Columns.Add(New DataColumn("ITEM"))
                dtDatos.Columns.Add(New DataColumn("CANTIDAD"))

                dtDatosMsj.Columns.Add(New DataColumn("ID"))
                dtDatosMsj.Columns.Add(New DataColumn("ASUNTO"))
                dtDatosMsj.Columns.Add(New DataColumn("PRIORIDAD"))
                dtDatosMsj.Columns.Add(New DataColumn("MENSAJE"))
                dtDatosMsj.Columns.Add(New DataColumn("REMITENTE"))
                dtDatosMsj.Columns.Add(New DataColumn("FECHA"))

                Me.GridMsj.DataBind()
                Me.GridMsj.DataSource = Me.dtDatosMsj

                Me.GridPersonal.DataBind()
                Me.GridPersonal.DataSource = Me.dtDatos

                If (contarSolicitudes() <> 0) Then
                    BtnAprobar.Visible = True
                End If

                If (contarMensajes() <> 0) Then
                    BtnAccion.Visible = True
                End If

                CargarSolicitudes()
                CargarMensajes()
            Catch
            End Try

        End If
        Me.GridPersonal.DataSource = Me.dtDatos
        Me.GridMsj.DataSource = Me.dtDatosMsj
        CargarDatos()

    End Sub

#Region "Funcionalidades"

    Private Function contarMensajes() As Integer
        Dim query As String = "select count(*) cuenta from mensajesistema where esleido ='N'"
        Dim cantidadReal As Integer = CInt(CL.Ejecuta(query).Item("cuenta").ToString)
        Return cantidadReal
    End Function

    Private Function contarSolicitudes() As Integer
        Dim query As String = "select count(*) cuenta from solicitudes where estado ='P'"
        Dim cantidadReal As Integer = CInt(CL.Ejecuta(query).Item("cuenta").ToString)
        Return cantidadReal
    End Function

    Private Sub CargarDatos()
        Dim usuario As Usuario = DirectCast(CL.ObtenerUsuarioUserID(Session("user").ToString).Item(0), Usuario)
        Dim empleado As Empleado = DirectCast(CL.obtenerEmpleadoFK(usuario.RefEmpleado).Item(0), Empleado)
        Me.LblNombre.Text = empleado.Nombre + " " + empleado.Apellido

        Dim query As String = "select count(*) cuenta from solicitudes where estado ='P'"
        Dim cantidadReal As Integer = CInt(CL.Ejecuta(query).Item("cuenta").ToString)

        Me.LblSolicitudes.Text = cantidadReal
        Me.LabelMensajesPendientes.Text = Me.contarMensajes()
        ImagePersonal.ImageUrl = empleado.RutaFoto

    End Sub

    Private Sub CargarMensajes()

        Dim arrayMensaje As ArrayList = CL.ObtenerMensajes()
        Dim i As Integer

        vaciaGrid()

        For i = 0 To arrayMensaje.Count - 1
            Dim drDatosMsj As DataRow = dtDatosMsj.NewRow
            Dim msj As Mensaje = DirectCast(arrayMensaje.Item(i), Mensaje)

            drDatosMsj.Item("ID") = msj.id
            drDatosMsj.Item("ASUNTO") = msj.Asunto
            drDatosMsj.Item("PRIORIDAD") = msj.Prioridad
            drDatosMsj.Item("MENSAJE") = msj.Cuerpo
            drDatosMsj.Item("REMITENTE") = msj.Remitente
            drDatosMsj.Item("FECHA") = msj.fecha


            dtDatosMsj.Rows.Add(drDatosMsj)

            Me.GridMsj.DataSource = dtDatosMsj
            Me.GridMsj.DataBind()
        Next
    End Sub

    Private Sub CargarSolicitudes()

        Dim arraySolicitud As ArrayList = CL.obtenerSolicitudesPendientes()
        Dim i As Integer

        vaciaGrid()

        For i = 0 To arraySolicitud.Count - 1
            Dim drDatos As DataRow = dtDatos.NewRow
            Dim sol As Solicitud = DirectCast(arraySolicitud.Item(i), Solicitud)

            Dim userSolicitante As Usuario = DirectCast(CL.ObtenerUsuarioFKID(sol.FkUsuariosSolicita).Item(0), Usuario)
            Dim trabajador As Empleado = DirectCast(CL.obtenerEmpleadoFK(userSolicitante.RefEmpleado).Item(0), Empleado)
            Dim material As Material = DirectCast(CL.ObtenerMaterialFK(sol.Fkmaterial).Item(0), Material)

            drDatos.Item("ID") = sol.ID
            drDatos.Item("FECHA") = sol.Fecha
            drDatos.Item("SOLICITANTE") = trabajador.Nombre + " " + trabajador.Apellido
            drDatos.Item("DETALLE") = sol.Descripcion
            drDatos.Item("CENTCOSTO") = sol.CentroCosto
            drDatos.Item("ITEM") = material.Nombre
            drDatos.Item("CANTIDAD") = sol.Cantidad

            dtDatos.Rows.Add(drDatos)

            Me.GridPersonal.DataSource = dtDatos
            Me.GridPersonal.DataBind()
        Next
    End Sub

    Private Sub vaciaGrid()
        dtDatos = DirectCast(Session("personal"), DataTable)
        If Not dtDatos Is Nothing Then
            dtDatos.Clear()
            Session("personal") = dtDatos
        End If
        Me.GridPersonal.DataSource = Nothing
        Me.GridPersonal.DataBind()
    End Sub

    Private Sub ActualizarSolicitudes()
        Dim filaGrid As GridViewRow

        With Me.GridPersonal
            For fila As Integer = 0 To .Rows.Count - 1
                filaGrid = .Rows(fila)
                Dim cb As CheckBox = filaGrid.FindControl("itemSelector")
                Dim cbR As CheckBox = DirectCast(filaGrid.FindControl("itemSelectorRe"), CheckBox)
                Dim ID As String = filaGrid.Cells(1).Text

                If cb.Checked = True Then
                    CL.ActualizarSolicitud(ID, "A")
                Else
                    If cbR.Checked = True Then
                        CL.ActualizarSolicitud(ID, "R")
                    End If
                End If
            Next
        End With
    End Sub

    Protected Sub BtnAprobar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnAprobar.Click
        ActualizarSolicitudes()
        CargarDatos()
        vaciaGrid()
    End Sub



#End Region

#Region "Eventos Grid"
    'Grid Material
    Protected Sub GridPersonal_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
        GridPersonal.PageIndex = e.NewPageIndex
        'Bind data to the GridView control.
        GridPersonal.DataSource = Session("personal")
        GridPersonal.DataBind()
    End Sub

    Protected Sub GridPersonal_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        'Set the edit index.
        GridPersonal.EditIndex = e.NewEditIndex
        'Bind data to the GridView control.
        GridPersonal.DataSource = Session("personal")
        GridPersonal.DataBind()
    End Sub

    Protected Sub GridPersonal_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        'Reset the edit index.
        GridPersonal.EditIndex = -1
        'Bind data to the GridView control.
        GridPersonal.DataSource = Session("personal")
        GridPersonal.DataBind()
    End Sub

    Protected Sub GridPersonal_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
        'Retrieve the table from the session object.
        Dim dt As DataTable = CType(Session("SubProyecto"), DataTable)

        'Update the values.
        Dim row As GridViewRow = GridPersonal.Rows(e.RowIndex)
        'Dim actividad As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text.ToString
        Dim FECHA As String = (CType((row.Cells(1).Controls(0)), TextBox)).Text
        Dim SOLICITANTE As String = (CType((row.Cells(2).Controls(0)), TextBox)).Text
        Dim DETALLE As String = (CType((row.Cells(3).Controls(0)), TextBox)).Text
        Dim CENTCOSTO As String = (CType((row.Cells(4).Controls(0)), TextBox)).Text
        Dim ITEM As String = (CType((row.Cells(5).Controls(0)), TextBox)).Text
        Dim CANTIDAD As String = (CType((row.Cells(6).Controls(0)), TextBox)).Text


        dt.Rows(row.DataItemIndex)("FECHA") = FECHA
        dt.Rows(row.DataItemIndex)("SOLICITANTE") = SOLICITANTE
        dt.Rows(row.DataItemIndex)("DETALLE") = DETALLE
        dt.Rows(row.DataItemIndex)("CENTCOSTO") = CENTCOSTO
        dt.Rows(row.DataItemIndex)("ITEM") = ITEM
        dt.Rows(row.DataItemIndex)("CANTIDAD") = CANTIDAD

        'Reset the edit index.
        GridPersonal.EditIndex = -1

        'Bind data to the GridView control.
        GridPersonal.DataSource = Session("personal")
        GridPersonal.DataBind()
    End Sub

    Sub GridPersonal_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
        Dim dt As DataTable = CType(Session("SubProyecto"), DataTable)
        dt.Rows(e.RowIndex).Delete()
        GridPersonal.DataSource = dt
        GridPersonal.DataBind()
    End Sub
#End Region

    
    
End Class
